package org.jeecg.modules.compilation.service.impl;

import cn.hutool.core.util.StrUtil;

import org.jeecg.modules.compilation.dto.DepartmentDTO;
import org.jeecg.modules.compilation.entity.CISysDepart;
import org.jeecg.modules.compilation.entity.CiDoc;
import org.jeecg.modules.compilation.service.ICISysDepartService;
import org.jeecg.modules.compilation.service.ICiDocService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

@Service
public class CiDepartmentServiceImpI {

    @Autowired
    private ICISysDepartService sysDepartService;
    @Autowired
    @Lazy
    private ICiDocService ciDocService;

//    public Boolean updateDepartment(){
//        final IDingMiniH5 app = DingAppFactory.app(DummyMiniH5App.class);
//        List<Response<Department>> departments = JdtDepartmentAPI.listAllResponse(DingAccessTokenFactory.access_token(app));
//        List<CISysDepart> list = new ArrayList<>();
//        for(Response<Department> department:departments) {
//            SysDepart depart = CiDepartmentTransfer.INSTANCE.toSysDepartment(department.getResult());
//            depart.setOrgCode("zsyc-" + UUID.randomUUID().toString().substring(0,8) );
//            list.add(depart);
//        }
//        return sysDepartService.saveBatch(list);
//    }

    public List<DepartmentDTO> queryList(){
        List<DepartmentDTO> list = new LinkedList<>();
//        List<CISysDepart> ciSysDepartList = sysDepartService.lambdaQuery().orderByAsc(CISysDepart::getShowNum).list();
        List<CISysDepart> ciSysDepartList = sysDepartService.lambdaQuery().list();
        for (CISysDepart ciSysDepart : ciSysDepartList) {
            DepartmentDTO departmentDTO = new DepartmentDTO();
            BeanUtils.copyProperties(ciSysDepart,departmentDTO);
            list.add(departmentDTO);
        }
        return list;
    }



    public Boolean editById(DepartmentDTO departmentVO) {
        List<String> num = new ArrayList<>();
        List<CISysDepart> list;
        CISysDepart depart = sysDepartService.lambdaQuery().eq(CISysDepart::getId,departmentVO.getId()).one();
        if (!departmentVO.getDepartName().equals(depart.getDepartName())) {
            ciDocService.lambdaUpdate()
                    .eq(CiDoc::getDepartmentId,departmentVO.getId())
                    .set(CiDoc::getDepartmentName,departmentVO.getDepartName())
                    .update();
        }
//        if ( Integer.parseInt(depart.getShowNum()) > Integer.parseInt(departmentVO.getShowNum()) ) {
//            for ( int i = Integer.parseInt(departmentVO.getShowNum()); i <Integer.parseInt(depart.getShowNum()); i++ )
//                num.add(String.valueOf(i));
//            list = sysDepartService.lambdaQuery().in(CISysDepart::getShowNum,num).list();
//            list.forEach( data -> {data.setShowNum( String.valueOf(Integer.parseInt(data.getShowNum()) + 1 ));});
//        } else {
//            for ( int i = Integer.parseInt(depart.getShowNum()); i <=Integer.parseInt(departmentVO.getShowNum()); i++ )
//                num.add(String.valueOf(i));
//            list = sysDepartService.lambdaQuery().in(CISysDepart::getShowNum,num).list();
//            list.forEach( data -> {data.setShowNum( String.valueOf(Integer.parseInt(data.getShowNum()) - 1 ));});
//        }
//        if ( list.size() != 0 )
//            sysDepartService.updateBatchById(list);

        return sysDepartService.lambdaUpdate()
                .set(StrUtil.isNotBlank(departmentVO.getDepartName()), CISysDepart::getDepartName, departmentVO.getDepartName())
//                .set(StrUtil.isNotBlank(departmentVO.getShowNum()), CISysDepart::getShowNum, departmentVO.getShowNum())
//                .set(StrUtil.isNotBlank(departmentVO.getIsShow()), CISysDepart::getIsShow, departmentVO.getIsShow())
                .eq(CISysDepart::getId, departmentVO.getId())
                .update();
    }


//    public List<DepartmentDTO> show(String isShow, String title) {
//        List<DepartmentDTO> result = new LinkedList<>();
//        List<CISysDepart> ciSysDepartList = sysDepartService.lambdaQuery()
//                .eq(StrUtil.isNotBlank(isShow), CISysDepart::getIsShow, isShow)
//                .like(StrUtil.isNotBlank(title), CISysDepart::getDepartName, title)
//                .list();
//        for (CISysDepart ciSysDepart : ciSysDepartList) {
//            DepartmentDTO departmentDTO = new DepartmentDTO();
//            BeanUtils.copyProperties(ciSysDepart,departmentDTO);
//            result.add(departmentDTO);
//        }
//        result.sort(Comparator.comparingInt(dataOne -> Integer.parseInt(dataOne.getShowNum())));
//        for (DepartmentDTO vo: result) {
//            if (StrUtil.isNotBlank(vo.getParentId()) || StrUtil.isNotBlank(vo.getParentId())) {
//                vo.setParentName( sysDepartService.lambdaQuery().eq(CISysDepart::getId,vo.getParentId()).one().getDepartName());
//            }
//        }
//       return result;
//    }


}
